package Controller;

import Model.*;
import java.util.*;

/*
 * Application logic to work with AddressBook and Person Classes
 */
public class AddressCtr
{
    //fields
    private Person person;
    private AddressBook addressBook;
    // initialize private field with ID for a each person. Going to be increased each time new user is created.
    // even after user is deleted, ID cannot be given to another one.
    private int uniqueID = 0;
    private static AddressCtr instance = null;
    
    // private constructor, implemented as singleton. We want to have only one place to manipulate all the data.
    private AddressCtr()
    {
        addressBook = AddressBook.getInstance();
    }
    
    public static AddressCtr getInstance()       
    {
        if(instance == null)
           {
               instance = new AddressCtr();
           }
        return instance;
    }

    // creates a person and adds it to the addressbook
    public void createPerson(String name,String address,String postalCode,String city,String phone)
    {
        person = new Person(uniqueID, name, address, postalCode, city, phone);
        addPerson(person);
        // increase the uniqueID after person has been added
        this.uniqueID++;
    }
    
    // method to add a person to addressbook
    public void addPerson(Person person)
    {
        addressBook = AddressBook.getInstance();
        addressBook.insertPerson(person);
    }
    
    // get a person by name, returns an ArrayList. Since there can be more with same name.
    public ArrayList<Person> getPerson(String name)
    {
        ArrayList<Person> foundList = new ArrayList<Person>();
        ArrayList<Person> searchList = AddressBook.getInstance().getAddressBook();
        for (Person person: searchList)
        {     
            if(person.getName().equals(name))
            {
                foundList.add(person);
            }
        }
        return foundList;
    }
    
    // returns ArrayList of persons in addressBook.
    public ArrayList<Person> getAllPersons()
    {
        return addressBook.getInstance().getAddressBook();
    }
    
    // updates person in addressBook. Specific ID have to be passed, because it's uniqe for each person.
    public void updatePerson(int id, String name, String address, String postalCode, String city, String phone)
    {
        // get an ArrayList addressBook from AddressBook model.
        ArrayList<Person> searchList = AddressBook.getInstance().getAddressBook();
        // search by specific ID, since it is unique.
        Person updatePerson = searchList.get(id);
        // if there isn't any information passed as a parameter the data in given field isn't changed
        if(!name.equals("")){
            updatePerson.setName(name);
        }
        if(!address.equals("")){
            updatePerson.setAddress(address);
        }
        if(!postalCode.equals("")){
            updatePerson.setPostalCode(postalCode);
        }
        if(!city.equals("")){
            updatePerson.setCity(city);
        }
        if(!phone.equals("")){
            updatePerson.setPhone(phone);
        }
    }
    
    // deletion of person from AddressBook
    public void deletePerson(int id)
    {
        boolean deleted = false;
        ArrayList<Person> searchList = AddressBook.getInstance().getAddressBook();
        Iterator<Person> it = searchList.iterator();
        while(!deleted&&it.hasNext())
        {
             Person person = it.next();
             if(person.getId()==(id));
             {
                 searchList.remove(person);
                 deleted = true;
             }
        }
         
         
    }
    
    // looks for a person by its specific ID.
    public Person findPerson(int personId)
    {
        return addressBook.getPerson(personId);
    }
}
